package com.jnu.myAccount.viewModel;

import android.annotation.SuppressLint;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.jnu.myAccount.menu.home.HomeViewModel;
import com.jnu.myAccount.record.FundAdapter;
import com.jnu.myAccount.record.Record;
import com.jnu.myAccount.record.DataPackage;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;


public class EditViewModel extends ViewModel {

    private final DataPackage items = DataPackage.Instance();

    private final MutableLiveData<String> type = new MutableLiveData<>();
    private final MutableLiveData<String> fund = new MutableLiveData<>();
    private final MutableLiveData<Boolean> InOrOut = new MutableLiveData<>();
    private final MutableLiveData<String> account = new MutableLiveData<>();
    private final MutableLiveData<String> date = new MutableLiveData<>();  //date: xx-yy-zz
    private final int[] dateList = new int[3];
    private Boolean needModify = false;
    private String modifyDate;
    private int modifyPosition;

    //构造函数：初始化记账类型、账户按钮，消费类型、金额文本框
    public EditViewModel() {
        InOrOut.setValue(true);
        account.setValue(items.getAccountList().get(0));
        fund.setValue("0.00");
        type.setValue(items.getKindTextList().get(0));

        Date dateToday = new Date(System.currentTimeMillis());
        @SuppressLint("SimpleDateFormat")
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        date.setValue(dateFormat.format(dateToday));

        //初始化年月日
        String[] strings = Objects.requireNonNull(this.date.getValue()).split("-");
        dateList[0] = Integer.parseInt(strings[0]);
        dateList[1] = Integer.parseInt(strings[1]);
        dateList[2] = Integer.parseInt(strings[2]);

    }
    public DataPackage getItems() {
        return items;
    }

    //获取消费类型总数
    public int getKindCount()
    {
        return items.getKindTextList().size();
    }

    //获取消费类型图标
    public int getKindIcon(int position) {
        return items.getKindIconList().get(position);
    }

    //获取消费类型名称
    public String getKindText(int position) {
        return items.getKindTextList().get(position);
    }

    public String getDayListString() {
        return dateList[0]+"-"+dateList[1]+"-"+dateList[2];
    }

    public void setType(int position) {
        type.postValue(items.getKindTextList().get(position));
    }

    public void setDateList(int year, int month, int day) {
        dateList[0]=year;
        dateList[1]=month;
        dateList[2]=day;
    }

    //根据年月日设置日期
    public void setDate(int year,int month,int day) {
        setDateList(year,month,day);
        date.postValue(getDayListString());
    }

    public void setInOrOut(Boolean inOrOut){
        InOrOut.setValue(inOrOut);
    }

    //设置点击收入支出按钮后文本值的变化
    public void setButtonInOut() {
        if (InOrOut.getValue()==null)
            return;
        InOrOut.setValue(!InOrOut.getValue());
    }

    //设置点击账户按钮后账户的变化(现金、微信、支付宝)
    public void setButtonAccount() {
        int index = items.getAccountList().indexOf(account.getValue())+1;
        if (index>= items.getAccountList().size())
            index=0;
        account.postValue(items.getAccountList().get(index));
    }

    public void setAccount(String myAccount) {
        account.postValue(myAccount);
    }

    public MutableLiveData<String> getType() {
        return type;
    }

    public MutableLiveData<String> getFund() {
        return fund;
    }

    public MutableLiveData<Boolean> getInOrOut() {
        return InOrOut;
    }

    public MutableLiveData<String> getAccount() {
        return account;
    }

    public MutableLiveData<String> getDate() {
        return date;
    }

    public Record getModify() {
        return items.getRecordNeedToModify();
    }

    public Boolean getNeedModify() {
        return needModify;
    }

    public String getModifyDate() {
        return modifyDate;
    }

    public int getModifyPosition() {
        return modifyPosition;
    }

    public void setNeedModify(Boolean needModify) {
        this.needModify = needModify;
    }

    public void setModifyDate(String modifyDate) {
        this.modifyDate = modifyDate;
    }

    public void setModifyPosition(int modifyPosition) {
        this.modifyPosition = modifyPosition;
    }

    public void completeFundFlowInfo() {
        Record info = new Record(
                account.getValue(), InOrOut.getValue(),//账户、收入支出
                date.getValue(), items.getKindTextList().indexOf(type.getValue()),//日期、消费类型名称
                Float.parseFloat(Objects.requireNonNull(fund.getValue())));//金额

        items.addNewRecord(info);
    }

    public void modify(Record recordNeedToModify) {
        recordNeedToModify.setAccount(account.getValue());
        recordNeedToModify.setInOrOutValue(InOrOut.getValue());
        recordNeedToModify.setDateValue(date.getValue());
        recordNeedToModify.setIndexOfType(items.getKindTextList().indexOf(type.getValue()));
        recordNeedToModify.setFundValue(Float.parseFloat(Objects.requireNonNull(fund.getValue())));
    }
}
